<template>
  <div class="home">
    <div class="header">
      <van-sticky>
        <van-nav-bar title="杰普点餐" />
      </van-sticky>
    </div>
    <div class="swipe">
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item @click="swiperClick" v-for="(item,index) in swiperData" :key="index">
          <img :src="item.url" alt="">
        </van-swipe-item>
      </van-swipe>
    </div>
    <div class="category">
      <van-grid :column-num="5">
        <van-grid-item @click="toProduct(item)" v-for="item in categoryData" :key="item.id" :icon="item.icon" :text="item.name" />
      </van-grid>
    </div>
    <div class="product">
      <van-card
        v-for="item in productData"
        :key="item.data"
        :price="item.price + '.00'"
        :desc="item.description"
        :title="item.name"
        :thumb="item.photo"
        @click="showDetail(item)"
      />
    </div>
    <div style="height: 100px;"></div>
    <van-action-sheet v-model="show" :title="productDetail.name">
      <div class="content">
        <img :src="productDetail.photo" alt="">
        <div>{{productDetail.name}}</div>
        <div>
          <span style="color: red;font-size: 20px;">{{productDetail.price}}元</span>
          /{{productDetail.unit}}
        </div>
        <div class="line"></div>
        <div>{{productDetail.description}}</div>
      </div>
    </van-action-sheet>
  </div>
</template>

<script>
import {get, post} from '@/http/axios.js'
import {mapActions, mapState} from 'vuex'
export default {
  data() {
    return {
      swiperData:[],
      productDetail: {},
      show: false
    }
  },
  created() {
    this.getSwiperData()
    this.getCategoryData()
    this.getProductData()
  },
  methods: {
    ...mapActions('category',['getCategoryDataVuex']),
    ...mapActions('product',['getProductDataVuex']),
    // 获取轮播图数据
    getSwiperData(){
      get('/carousel/findAll').then(res=>{
        // console.log(res);
        this.swiperData = res.data.data
      })
    },
    swiperClick(event){
      console.log(event);
    },
    // 获取所有栏目数据
    getCategoryData(){
      let param = {
        page: 1,
        pageSize: 200
      }
      this.getCategoryDataVuex(param)
    },
    // 获取产品数据
    getProductData(){
      let param = {
        page:1,
        pageSize: 10
      }
      this.getProductDataVuex(param)
    },
    showDetail(product){
      console.log(product);
      this.productDetail = product
      this.show = true
    },
    toProduct(item){
      let activeKey = item.num - 1
      this.$router.push({
        path:'/manager/product',
        query:{
          productCategoryId: item.id,
          activeKey
        }
      })
    }
  },
  computed:{
    ...mapState('category',['categoryData']),
    ...mapState('product',['productData'])
  }
};
</script>

<style scoped>
.swipe img{
  width: 100%;
  height: 200px;
}
.content{
  width: 100%;
  height: 400px;
  text-align: center;
}
.content img{
  width: 50%;
}
.content .line{
  border: 1px solid red;
  margin: 20px;
}
</style>